﻿Imports System.Web.Mvc
Imports Equipment.Business
Imports System.Web.Security

Public Class RequiresAuthenticationAttribute : Inherits ActionFilterAttribute
    Public Overrides Sub OnActionExecuting(ByVal filterContext As System.Web.Mvc.ActionExecutingContext)
        If Not Application.CurrentUserInfo.IsAuthenticated Then
            Dim redirectOnSuccess As String = filterContext.HttpContext.Request.Url.AbsolutePath
            Dim redirectUrl As String = String.Format("?ReturnUrl={0}", redirectOnSuccess)
            Dim loginUrl As String = FormsAuthentication.LoginUrl + redirectUrl
            If filterContext.HttpContext.User.Identity.IsAuthenticated Then
                FormsAuthentication.SignOut()
            End If

            filterContext.HttpContext.Response.Redirect(loginUrl, True)
        End If
    End Sub
End Class